package com.swd2010.mobileSearch;

import java.util.ArrayList;
import java.util.List;

import com.swd2010.mobileSearch.data.PersonBean;
import com.swd2010.mobileSearch.data.adapter.PersonBeanForListView;
import com.swd2010.mobileSearch.data.internalTransfer.ApplicationDataTransfer;
import com.swd2010.mobileSearch.event.ShakeListener;
import com.swd2010.mobileSearch.network.AutoCompleteAdapter;
import com.swd2010.mobileSearch.network.PersonByIdWebservice;
import com.swd2010.mobileSearch.network.PersonSearchWebservice;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;



public class PersonenActivity extends Activity implements OnItemClickListener, OnClickListener {
	
	
	private AutoCompleteTextView  aTV;
	
	public ListView personenSuchResultListView;
	
	//private PersonBean selectPersonFromResultListPersonBean;
	
	private Button suchenButton;
	
	
	 private ShakeListener shakeListener;
	
	
	
	
    public void onCreate(Bundle savedInstanceState) {
    	
    	
        super.onCreate(savedInstanceState);
        
                
        setContentView(R.layout.personen_layout);
        
        
                

        shakeListener = new ShakeListener(this);
        
        shakeListener.setOnShakeListener(new ShakeListener.OnShakeListener () {
        	
        	
          public void onShake()
          {
                                        
              SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
                      
              String shakeDetection = sharedPref.getString(getString(R.string.ShakeDetection), "Y");        	  
        	  
              
              if (shakeDetection.equals("Y")){
            	  
            	  Toast.makeText(getBaseContext(), "Auf ein Neues :-).", Toast.LENGTH_SHORT).show();

            	  PersonenActivity.this.aTV.setText("");
            	  
              }
            
          }
          
        }
        
        );        
        
        
        
        personenSuchResultListView = (ListView) findViewById(R.id.personenSuchResultListId);
        
        
        
        suchenButton = (Button) findViewById(R.id.button1);
        
        suchenButton.setOnClickListener(this);
        
        
        //Haui dann bei den Räumen die id anpassen
        aTV = (AutoCompleteTextView) findViewById(R.id.personenSuchFeldId);        
        
        
        aTV.requestFocus();
        
        
        //Haui hier könntest du dann von außen übergeben
        //Working
        //aTV.setAdapter(new PlacesAutoCompleteAdapter(this,R.layout.personen_layout,R.id.personenSuchFeldId,PersonSearchAutoCompleteWebservice.autocomplete("Test")));
        
        
        aTV.setAdapter(new AutoCompleteAdapter(this, R.layout.item_list,R.id.autoCompleteId,null));
        
        //aTV.setAdapter(new PlacesAutoCompleteAdapter(this, android.R.layout.simple_list_item_1));

                
        aTV.setOnItemClickListener(this);
        

        
		
        
    }


	public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {


		//selectPersonFromResultListPersonBean = new PersonBean();
		
//		Log.e("AdapterView", "Integer.toString(adapterView.getId())");
		
//		Log.e("AdapterView", Integer.toString(adapterView.getId()));
		
		if ( adapterView.getId() == R.id.personenSuchResultListId ){

			
			PersonBeanForListView pBV = (PersonBeanForListView) personenSuchResultListView.getItemAtPosition(position);
			
			showPersonDetailActivity(pBV.getId());
			
		}
		else{

			
			String str = (String) adapterView.getItemAtPosition(position);


			this.doShowPersonSearchResutls(str);

			
		}
	
        
	}
	
	
	private void showPersonDetailActivity(String personId){
		
		
		
		Intent callPersonDetailActivityIntent = new Intent(PersonenActivity.this, PersonenDetailActivity.class);
		
		callPersonDetailActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				
		/**
		Intent intent = new Intent(mContext, SomeActivity.class);
		intent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK);
		..
		...
		..

		mContext.startActivity(intent);		
		**/
		
		ApplicationDataTransfer.getInstance().setCurrentWorkingPerson( PersonByIdWebservice.get( personId ) );
		
		startActivity(callPersonDetailActivityIntent);		

		
	}
	
	
	public void doShowPersonSearchResutls(String   searchTerm){
		
		
		List<PersonBean>  lpB = PersonSearchWebservice.get(searchTerm);
		
		PersonBeanForListView[] values = new PersonBeanForListView[lpB.size()];
//Log.e( Integer.toString( lpB.size() ), Integer.toString( lpB.size() ));		
		int counter = 0;
		
/**
		PersonBean pBx = new PersonBean();
		
		pBx.setFullNameWithDegress("No data found");
		
		values[0] = new PersonBeanForListView(pBx);
	**/		

		
		//Leeren der Anzeigeliste
		personenSuchResultListView.setAdapter( new ArrayAdapter<String>( this
					, android.R.layout.simple_list_item_1
					, android.R.id.text1
					, new String[] {""}
			) );

		
		
		for ( PersonBean pB : lpB ){
									
			values[counter] = new PersonBeanForListView(pB);
			
			counter++;
			
			//Log.e("id ", pB.getAdditionalInformation() + " id");

		}
		
		
		


//Log.e("length", Integer.toString( values.length ));		
		if (values.length != 0){
					
			
			ArrayAdapter<PersonBeanForListView> adapter = new ArrayAdapter<PersonBeanForListView>( this
					, android.R.layout.simple_list_item_1
					, android.R.id.text1
					, values
			);


			// Assign adapter to ListView
			personenSuchResultListView.setAdapter(adapter);
		
			personenSuchResultListView.setOnItemClickListener(this);
			
		}
		else{
			
			
			Toast myMeal = Toast.makeText(this, "Keine Einträge gefunden.",Toast.LENGTH_LONG);
			
			myMeal.show();
			
			
		}		
		
		
	}


	public void onClick(View arg0) {
//Log.e("Klick","Klick");		
		doShowPersonSearchResutls( aTV.getText().toString());		
		
	}



	
	
}
